package weaver.interfaces.cese.duss.Test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import weaver.general.Util;
import weaver.interfaces.cese.duss.ceseutil.BDC;

import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-01-25
 * @Description: ${description}
 * @ModifyLog:
 **/

public class ImageCutTest {
    public static String httpPost(String path) {
        String result = "";
        try {
            File file = new File( path );
            URL url = new URL( "https://api.inspuronline.com/ai/ocr/v1/receipt/classify" );
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            con.setDoOutput( true );
            con.setDoInput( true );
            con.setRequestMethod( "POST" );
            con.setUseCaches( false );
            con.setRequestProperty( "MediaType", "application/octet-stream" );
            con.setRequestProperty( "Content-Type", "application/octet-stream" );
            con.setRequestProperty( "X-API-Key", "52f19a5e-622f-ed63-1a23-2e0407ee1e2b" );
            OutputStream out = con.getOutputStream();
            //传入参数
            FileInputStream inputStream = new FileInputStream( file );
            byte[] data = new byte[2048];
            int len = 0;
            while ((len = inputStream.read( data )) != -1) {//将读取到的本地文件流读取到HttpsURLConnection,进行上传
                out.write( data, 0, len );
            }
            out.flush();
            inputStream.close();
            out.close();
            int code = con.getResponseCode(); //获取post请求返回状态
            System.out.println( "------------ORC接口返回CODE=" + code );
            BufferedReader br = new BufferedReader( new InputStreamReader( con.getInputStream(), "UTF-8" ) );
            result = br.readLine();
            //获取请求的资源
            con.disconnect();//断开HttpsURLConnection连接
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }


    public static String imageCut(String filepath){
       return "";
    }
    public static void main(String[] args) {
        String srcpath = "D:\\download\\fplx\\2333.jpg";
        BufferedImage sourceImg = null;
        try {
            File picute = new File( srcpath );
            String picuteName = picute.getName();
            sourceImg = ImageIO.read( new FileInputStream(picute) );
            String result = httpPost( srcpath );
            JSONObject resultJson = JSONObject.parseObject( result );
            String code = resultJson.getString( "status" );
            if("200".equals( code )){
                JSONObject data = JSONObject.parseObject( resultJson.getString( "data" ) );
                String boxes = data.getString( "boxes" );
                JSONArray dataArray = JSONArray.parseArray( boxes );
                System.out.println(dataArray.size());
                if(dataArray.size()>1){
                    for (int i = 0; i < dataArray.size(); i++) {
                        JSONObject results = (JSONObject) dataArray.get( i );
                        String box = results.getString( "box" );
                        String[] arr = StringUtils.strip(box,"[]").split( "," );
                        String y0 = BDC.floatMultiply( arr[0], sourceImg.getHeight() + "",0);
                        String x0 = BDC.floatMultiply( arr[1], sourceImg.getWidth() + "",0);
                        String y1 = BDC.floatMultiply( arr[2], sourceImg.getHeight() + "",0);
                        String x1 = BDC.floatMultiply( arr[3], sourceImg.getWidth() + "",0);
                        String tag = results.getString( "tag" );
                        String confidence = results.getString( "confidence" );
                        String box_id = results.getString( "box_id" );
                        int x = Util.getIntValue( x0 );
                        int y = Util.getIntValue( y0 );
                        int width = Util.getIntValue( x1 ) - Util.getIntValue( x0 );
                        int heigth = Util.getIntValue( y1 ) - Util.getIntValue( y0 );
                        System.out.println("x="+x);
                        System.out.println("y="+y);
                        System.out.println("width="+width);
                        System.out.println("heigth="+heigth);
//                    OperateImage operateImage = ImageUtil( x, y, width, heigth );
//                    operateImage.srcpath = srcpath;
//                    operateImage.subpath = "D:\\download\\fplx\\new"+i+".jpg";
//                    operateImage.cut();
                    }
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
